/// @tags: Kruskal
#include <algorithm>
#include <cstdio>
#include <iostream>
#define rint register int

using std::cin;
using std::cout;

namespace BlueQuantum {

typedef long long LL;

constexpr int maxn = 1e5 + 3, maxm = 3e5 + 3, lg = 19;
constexpr LL inf = 0x3f3f3f3f3f3f3f3f;

struct Edge {
  int u, v, w;
  inline bool operator<(const Edge &rhs) const { return w < rhs.w; }
} pe[maxm];

struct GEdge {
  int nxt, v, w;
} e[maxn * 2];

int heads[maxn];
int fa[maxn], use[maxm];
int f[maxn][lg], mx1[maxn][lg], mx2[maxn][lg];
LL dep[maxn];

int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }

inline void add(int u, int v, int w) {
  static int ecnt = 0;
  e[++ecnt].v = v, e[ecnt].nxt = heads[u], e[ecnt].w = w, heads[u] = ecnt;
}

void dfs(int u, int fa) {
  for (int i = heads[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (v != fa) {
      dep[v] = dep[u] + 1;
      mx1[v][0] = e[i].w;
      f[v][0] = u;
      dfs(v, u);
    }
  }
}

void update(int &mx1, int &mx2, int mxx1, int mxx2) {
  int max = std::max(mx1, mxx1), smax = 0;
  if (mxx1 > mx1) {
    smax = std::max(mxx2, mx1);
  } else if (mxx1 < mx1) {
    smax = std::max(mxx1, mx2);
  }
  mx1 = max, mx2 = smax;
}

void build(LL n) {
  for (int j = 1; j < lg; ++j)
    for (int i = 1; i <= n; ++i) {
      f[i][j] = f[f[i][j - 1]][j - 1];
      mx1[i][j] = mx1[i][j - 1];
      mx2[i][j] = mx2[i][j - 1];
      update(mx1[i][j], mx2[i][j], mx1[f[i][j - 1]][j - 1], mx2[f[i][j - 1]][j - 1]);
    }
}

inline int solve(const Edge &e) {
  int u = e.u, v = e.v, w = e.w, max1 = 0, max2 = 0;
  if (dep[u] < dep[v]) std::swap(u, v);
  for (int i = lg - 1; i >= 0; --i) {
    if (dep[f[u][i]] >= dep[v]) {
      update(max1, max2, mx1[u][i], mx2[u][i]);
      u = f[u][i];
    }
  }
  for (int i = lg - 1; i >= 0; --i) {
    if (f[u][i] != f[v][i]) {
      update(max1, max2, mx1[u][i], mx2[u][i]);
      update(max1, max2, mx1[v][i], mx2[v][i]);
      u = f[u][i];
      v = f[v][i];
    }
  }
  if (u != v) {
    update(max1, max2, mx1[u][0], mx2[u][0]);
    update(max1, max2, mx1[v][0], mx2[v][0]);
    u = f[u][0];
    v = f[v][0];
  }
  if (max1 == w) {
    return max2;
  } else
    return max1;
}

inline int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> pe[i].u >> pe[i].v >> pe[i].w;
  }
  std::sort(pe + 1, pe + m + 1);
  for (int i = 1; i <= n; ++i) fa[i] = i;
  LL sum = 0, ans = inf;
  for (int i = 1; i <= m; ++i)
    if (find(pe[i].u) != find(pe[i].v)) {
      add(pe[i].v, pe[i].u, pe[i].w);
      add(pe[i].u, pe[i].v, pe[i].w);
      sum += pe[i].w;
      fa[find(pe[i].u)] = find(pe[i].v);
      use[i] = true;
    }
  dfs(1, 1);
  build(n);
  for (int i = 1; i <= m; ++i)
    if (!use[i]) ans = std::min(ans, sum + pe[i].w - solve(pe[i]));
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P4180 [BJWC2010] 严格次小生成树.in", "r", stdin);
  freopen("P4180 [BJWC2010] 严格次小生成树.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
